package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/emLogin")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session){
        JsonResult jsonResult = new JsonResult();
        Employee employee = employeeService.checkUser(username, password);
        session.setAttribute("USER_IN_SESSION",employee);
        List<Permission> permissions = employeeService.selectPermissionByEmpId(employee.getId());
        session.setAttribute("PERMISSION_IN_SESSION",permissions);
        return jsonResult;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    public String errorMsg(){
        return "common/nopermission";
    }

    @RequestMapping("/msgError")
    public String msgError(){
        return "common/error";
    }
}
